package com.nchuoj.judger.uitl;

import com.feign.exceptions.CustomException;
import com.feign.util.ShellRun;
import com.feign.util.pojo.ShellRunData;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.concurrent.CompletionException;

/**
 * TODO
 *
 * @Description 继承于 ShellRun类的扩展类 主要业务用于运行java代码
 * @Author ZW
 * @Date 2022/12/31 20:44
 **/
@Component
public class JavaRunShell extends ShellRun {
    /**
     * 运行的java代码位置
     *
     * @param codePath java代码路径
     * @return 代码运行情况
     */
    final String javaExePath = "D:\\Path\\jdk-17.0.5\\bin\\java.exe";
    /**
     * 搭配的java运行脚本参数
     */
    final String arguments = "-Djava.security.manager";
    final String input = "<";
    final String output = ">";

    public ShellRunData runJavaCode(String codePath,String inputFilePath) {
        //文件不存在返回未运行的空数据
        if (Boolean.FALSE.equals(new File(codePath).exists())||Boolean.FALSE.equals(new File(codePath).exists())) {
//            return ShellRunData.notRunData();
            new File(codePath).delete();
            throw new CustomException("代码文件丢失或输入丢失");
        }
        return runShell(javaExePath, codePath,input,inputFilePath);
    }
}
